﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core;
using Baosight.Platform.Core.ServiceModel;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class LimitsForPeople : EFViewCmes
    {
        public LimitsForPeople(String _dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(_dialogType);
        }
        
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.peopleWindows.GetChildObjects<EFDataField>(null, true);
            _view.Args.Add("fdDepotId", this.fdDepotId.GetDataValue().ToString());
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 给查看权限默认赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnAddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            UIBaseEntity temp = e.NewObject as UIBaseEntity;
            EIRow _row = temp.GetEIRow();

            if (null != _row)
            {
                _row["fdViewPermission"] = "1";
            }

        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                default:
                    break;
            }
        }

        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {
            if (this.ValidateField())
            {
                Button button = sender as Button;
                button.IsEnabled = false;
                TempButton = button;
                if (null == this.grid.SelectedItem)
                {
                    base.OpenEFWindowAlert("请添加一条记录进行操作", true);
                }
                else
                {
                    EIInfo eiInfo = new EIInfo();
                    EIBlock eiBlock = this.grid.EIBlock == null ? new EIBlock("detailsResult") : this.grid.EIBlock;
                    eiInfo.AddBlock(eiBlock);

                    List<string> _list = new List<string>();
                    for (int i = 0; i < this.EIDataContext.GetBlock("peopleList").GetRowCount(); i++)                    
                    {
                        _list.Add(this.EIDataContext.GetBlock("peopleList").GetRow(i)["fdPeopleId"].ToString());
                    }

                    for (int i = 0; i < eiBlock.GetRowCount(); i++)
                    {
                        if (eiBlock.GetRow(i)["fdPeopleId"] == null || eiBlock.GetRow(i)["fdPeopleId"].ToString() == "" || eiBlock.GetRow(i)["fdPeopleId"].Equals(""))
                        {
                            base.OpenEFWindowAlert("请选择要新增的人员信息。", true);
                            return;
                        }
                        else
                        {
                            if (_list.Contains(eiBlock.GetRow(i)["fdPeopleId"].ToString()))
                            {
                                base.OpenEFWindowAlert("选择的新增人员有重复！", true);
                                return;
                            }
                            else
                            {
                                _list.Add(eiBlock.GetRow(i)["fdPeopleId"].ToString());
                            }
                        }
                    }
                    eiInfo.Attributes.Add(EIConstants.ServiceNameKey, "ZMCM12");
                    eiInfo.Attributes.Add(EIConstants.MethodNameKey, "insert");
                    eiInfo.Attributes.Add("fdDepotId", this.fdDepotId.Value);


                    base.Submit(eiInfo, DoInsertOperationCompleted);
                }
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private Dictionary<string, EIColumn> CreateColumns()
        {
            Dictionary<string, EIColumn> columns = new Dictionary<string, EIColumn>();
            EIColumn _column = new EIColumn();
            _column.Name = "fdPeopleId";
            _column.Desc = "人员";
            columns.Add("fdPeopleId", _column);

            _column = new EIColumn();
            _column.Name = "fdManagmentPermission";
            _column.Desc = "管理权限";
            columns.Add("fdManagmentPermission", _column);

            _column = new EIColumn();
            _column.Name = "fdViewPermission";
            _column.Desc = "查看权限";
            columns.Add("fdViewPermission", _column);

            return columns;
        }

        /// <summary>
        /// 新增成功后回调
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DoInsertOperationCompleted(object sender, DoWorkCompletedEventArgs e)
        {
            EIInfo eIInfo = e.EIInfo;
            if (eIInfo.Status == -1)
            {
                this.TempButtonOpen(true);
                base.OnCommitMessagePopup(eIInfo);//操作失败，消息提醒
            }
            else
            {
                //操作成功关闭新增页面
                //此处可以扩展是否连续新增
                EFWindow window = this.Parent as EFWindow;
                window.DialogResult = true;
                window.Close();
            }

        }
        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 新增时给查看权限默认赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_AddingNewDataItem(object sender, GridViewAddingNewEventArgs e)
        {
            UIBaseEntity temp = e.NewObject as UIBaseEntity;
            EIRow _row = temp.GetEIRow();

            if (null != _row)
            {
                _row["fdViewPermission"] = "1";
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void grid_CellEditEnded(object sender, GridViewCellEditEndedEventArgs e)
        {
             //IEFColumn column = e.Cell.Column as IEFColumn;
             //if (column != null)
             //{
             //    if (column.eColumnName.Equals("fdPeopleName"))
             //    {
             //        column = e.Cell.Column as EFGridCustomViewColumn;
             //        EFCustomDataPicker picker = (e.Cell.Content as GridViewEditorPresenter).Content as EFCustomDataPicker;
             //        UIBaseEntity obj = e.Cell.ParentRow.DataContext as UIBaseEntity;
             //        EIRow row = obj.GetEIRow();
             //        if (picker.ItemObject != null)
             //        {
             //            row["fdPeopleId"] = picker.ItemObject.GetType().GetProperty("empCode").GetValue(picker.ItemObject, null);
             //        }
             //    }
             //}
        }

    }
}
